/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import javax.persistence.*;
import jpapractica.*;

/**
 *
 * @author master
 */
public class ArtistManager {
    
    EntityManagerFactory emf;
    EntityManager managerArtist;
    
    public ArtistManager(){
        emf= Persistence.createEntityManagerFactory("GRLUPU");
        managerArtist= emf.createEntityManager();
    }
    
    public Artist create (int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        
        Artist artista = new Artist(id, name, lastname, picture, birthplace,dia,mes,ano);
        
      
        managerArtist.getTransaction().begin();
        managerArtist.persist(artista);
        managerArtist.getTransaction().commit();
        
        return artista;
        
    }
    public List<Artist> buscar(String palabra){
   
        Query busqueda =managerArtist.createQuery("SELECT p FROM Person p WHERE p.name='"+palabra+"'");
        List<Artist> aux =busqueda.getResultList();
         return aux;
    }
    public void update(int id, String name, String lastname, String picture,
                            String birthplace, int dia, int mes, int ano){
        Artist artista = managerArtist.find(Artist.class, id);
        managerArtist.getTransaction().begin();
        artista.setName(name);
        artista.setLastname(lastname);
        artista.setPicture(picture);
        artista.setBirthplace(birthplace);
        artista.setBirthdate(dia,mes,ano);
        managerArtist.getTransaction().commit();    
    
    }
    public void delete (int id){
        Artist artist = managerArtist.find(Artist.class, id);
        
        managerArtist.getTransaction().begin();
        managerArtist.remove(artist);
        managerArtist.getTransaction().commit();
    }
    
}
